package com.sinosoft.jike;

import com.sinosoft.jike.dto.Rule;
import com.sinosoft.jike.dto.RuleNormal;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class Args<T> {
    String args;
    Map<String, String> ruleList;
    String errMsg;
    T result;

    /**
     * 指令拆分
     * @return
     */
    public boolean generateRuleMap(){
        boolean result = true;
        if (args != null && !args.equals("")){
            String[] tmpList = args.split(" ");
            String nowSign = null;
            for (String tmp: tmpList){
                if (tmp.startsWith("-")){
                    nowSign = tmp.replaceFirst("-", "");
                    ruleList.put(nowSign, "");
                } else if (nowSign != null){
                    // 每一次给指令赋值，都会将当前指令置空
                    ruleList.put(nowSign, tmp);
                    nowSign = null;
                } else {
                    setErrMsg(tmp+"不符合指令策略");
                    result = false;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 指令解析
     * @return
     */
    public boolean commandParser(String key, String value, Object obj){
        Class clazz = obj.getClass();
        Field fields = null;
        try {
            fields = clazz.getDeclaredField(key);
        } catch (NoSuchFieldException e) {
            setErrMsg(key+"指令不存在");
            return false;
        }

        // 按规则设置默认参数
        if (value.equals("")){
            Rule rule = (Rule) obj;
            value = rule.getDefaultValue(key);
        }

        fields.setAccessible(true);
        try {
            if (fields.getType().equals(Integer.class)){
                fields.set(obj, Integer.valueOf(value));
            }else if (fields.getType().equals(Float.class)){
                fields.set(obj, Float.valueOf(value));
            }else if (fields.getType().equals(Boolean.class)){
                fields.set(obj, Boolean.valueOf(value));
            }
        } catch (Exception e) {
            setErrMsg(key+":"+value+"指令与参数不匹配");
            return false;
        }

        setResult((T) obj);
        return true;
    }

    /**
     * 解析的目标对象
     * @param obj
     * @return
     */
    public boolean parse(Object obj){
        if (generateRuleMap()){
            for(Map.Entry<String, String> entry : ruleList.entrySet()) {
                if (!commandParser(entry.getKey(), entry.getValue(), obj)){
                    return false;
                }
            }
        }

        return false;
    }

    public Args(String args) {
        this.args = args;
        this.ruleList = new HashMap<String, String>();
    }

    public String getArgs() {
        return args;
    }

    public void setArgs(String args) {
        this.args = args;
    }

    public Map<String, String> getRuleList() {
        return ruleList;
    }

    public void setRuleList(Map<String, String> ruleList) {
        this.ruleList = ruleList;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public static void main(String[] args) {
        Args<RuleNormal> arg = new Args("-integer 123 123 -aFloat 12.5 -aBoolean false");
        if (arg.parse(new RuleNormal())){
            System.out.println(arg.getResult().getInteger());
            System.out.println(arg.getResult().getaFloat());
            System.out.println(arg.getResult().getaBoolean());
        }else{
            System.out.println(arg.getErrMsg());
        }
    }
}
